/***
 *
 *一个用于分解模型的类 Disassemble.js
 * 可实现拖拽模型或者说动画移动
 *
 * Daniel
 *
 * 2018.03.26
 *
 ***/

define([],function(){
    var Split = function(){
        this.world = {};
        this.group = [];
        
        this.changeId = [];
        this.changeModel = [];
    
    };
    
    //初始化默认参数
    Split.prototype.init = function(world){
            this.world = world;
            this.group = world.modelShell;
            this.tween = createjs.Tween;
            this.Animate = createjs.Ease.linear;
    };

    //分解模型
    Split.prototype.animations = function(points,time){
        var object = this.group;
        var times = time || 1000;
        var i = object.length-1;
        var Animate = this.Animate;
        var anime = this.tween;
        for(;i>-1;i--){
            var position;
            points !== undefined ?  position = points.clone() : position = this.computePoint();
            var target = object[i].position;
            anime.get(target).to(position, times, Animate);
        }
    };
    
    

    //聚合模型
    Split.prototype.resetModel = function(time){
        var origin = new THREE.Vector3();
        this.animations(origin,time);
    };
    
    //滑动变换
    Split.prototype.setPosition = function(object,value,ref){
        
        var mix,min,x,y,z,position,target,id,flag;
            mix = 100;
            min = -100;
        target= object.position;
        id = object.id;
        flag = this.changeId.indexOf(id);
        if(flag == -1 && ref){
            this.changeModel.push(object);
        };
        position = new THREE.Vector3(value.x,value.y,value.z).clone();
        x = position.x;
        y = position.y;
        z = position.z;
        target.set(0,0,0);
        target.addScaledVector(position,100);
    if(z == 0){
        if(x==mix ||x==min || y==mix || y==min){
            
            //将模型降到同一水平面
            
           // target.setZ(position.z);
        }
    }
    };
    
    //重置功能
    Split.prototype.resetPosition = function(){
        //执行代码
        var object  = this.changeModel;
        var i = object.length-1;
        var original = new THREE.Vector3();
        for(;i>-1;i--){
            this.setPosition(object[i],original);
        }
        this.changeModel = [];
        this.changeId = [];
    };
    
    //随机数生产器
    Split.prototype.computePoint = function(cooder){
        var num = Math.round(Math.random()*10);
        var point = new THREE.Vector3();
        var value = cooder || 3;
        var x = (Math.random()*num*value*1000).toFixed(2);
        var y = (Math.random()*num*value*1000).toFixed(2);
        var z = (Math.random()*num*value*1000).toFixed(2);
        point.set(x,y,z);
        return point;
    };
    
    
    
    
    
    
    return Split;
});